<%@ page import="com.example.whopxx.domain.User" %>
<%@ page import="java.net.URLDecoder" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>用户管理</title>
    <link rel="stylesheet" href="./css/manager.css">
    <link rel="stylesheet" href="./css/bootstrap.min.css" />
</head>
<body>
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                    data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">用户管理</a>
        </div>
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
                <li><a href="#">
                    <%
                        User user = (User) session.getAttribute("user");
                        if (user == null) {
                            response.sendRedirect("login.jsp");
                            return;
                        }
                    %>
                    <%=user.getUsername() %>
                    欢迎你，
                    ${ (! empty cookie.LastTime.value) ?"您上一次的登录时间是：".concat(URLDecoder.decode(cookie.LastTime.value, "utf-8"))
                    : "这是您首次登录" }
                    <%
                        Cookie[] cookies = request.getCookies();
                        boolean flag = false;
                        if(cookies != null) {
                            for (Cookie cookie : cookies) {
                                String name = cookie.getName();
                                if ("LastTime".equals(name)) {
                                    // 不是第一次访问
                                    flag = true;
                                    String value = cookie.getValue();
                                    value = URLDecoder.decode(value, "utf-8");
                                    System.out.println("欢迎回来，您上次访问的时间为：" + value);
                                    Date date = new Date();
                                    SimpleDateFormat timmesdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                                    String str_time = timmesdf.format(date);
                                    str_time = URLEncoder.encode(str_time, "utf-8");
                                    cookie.setValue(str_time);
                                    cookie.setMaxAge(60 * 60 * 24);
                                    response.addCookie(cookie);
                                    break;
                                }
                            }
                        }
                        if(cookies == null ||  cookies.length == 0 || !flag){
                            Date date = new Date();
                            SimpleDateFormat timmesdf = new SimpleDateFormat("yyyy年MM月dd HH:mm:ss");
                            String str_time = timmesdf.format(date);
                            System.out.println(str_time);
                            str_time = URLEncoder.encode(str_time, "utf-8");
                            System.out.println(str_time);
                            Cookie cookie = new Cookie("LastTime", str_time);
                            cookie.setMaxAge(60*60*24);
                            response.addCookie(cookie);
                            String decode = URLDecoder.decode(str_time, "utf-8");
                            System.out.println("欢迎您首次访问，您上次访问的时间为："+decode);
                        }
                    %>
                </a>
                </li>
                <li> <a href="LogoutServlet">注销</a></li>
            </ul>
        </div>
    </div>
    <div class="container">
        <table class="table">
            <thead>
            <tr>
                <th>
                    序号
                </th>
                <th>
                    用户名
                </th>
                <th>
                    姓名
                </th>
                <th>
                    性别
                </th>
                <th>
                    电话
                </th>
                <th>
                    邮箱
                </th>
                <th>
                    证件类型
                </th>
                <th>
                    证件号码
                </th>
                <th>
                    操作
                </th>
            </tr>
            </thead>
            <c:forEach var="user" items="${list}">
                <tr>
                    <td>${user.id}</td>
                    <td>${user.username}</td>

                    <td>${user.truename}</td>

                    <td>
                            ${user.gender}
                    </td>
                    <td>
                            ${user.phone}
                    </td>
                    <td>
                            ${user.email}
                    </td>
                    <td>
                            ${user.card}
                    </td>
                    <td>
                            ${user.cardNo}
                    </td>
                    <td>
                        <a href="DeleteServlet?id=${user.id}">删除</a>
                        <a href="UpdateServlet?id=${user.id}">修改</a>
                    </td>
                </tr>
            </c:forEach>
        </table>
    </div>
</body>
</html>